// 这里写发送请求prompts的方法和消息存储
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getGptResponse } from '@/apis/gpt.js'

export const usePromptsStore = defineStore('prompts', () => {
  // 1. 需要一个列表来保存用户和GPT的对话
  // type: messages: [ { role: 'user', content: 'XXX' }，{ role: 'assistant', content: 'XXX' } ]
  const initMessage = [
    {
      role: 'user',
      content: '你是一个少儿编程课堂的老师,你只能回答有关编程的问题。如果我问你非编程的问题，请你提醒我专心学习编程!另外请你将回复的字数限制在50字以内,尽量简洁不罗嗦。'
    },
    {
      role: 'assistant',
      content: '好的,我现在是一名少儿编程老师。如果你问我其他问题,我会提醒你好好学习编程。同时我会尽可能简洁的回复你。'
    }
  ]
  const messagesList = ref({
    messages: initMessage
  })

  const addUserMessage = (msg) => {
    const newMsg = {
      role:'user',
      content:msg
    }
    messagesList.value.messages.push(newMsg)
    // console.log('成功加入对话列表...', messagesList.value.messages)
  }

  // 发送消息
  const sentMsg = async () => {
    // return messagesList.value.messages
    disabled.value = true
    const res = await getGptResponse(messagesList.value)
    disabled.value = false
    return res
  }

  const addAssistantMessage = (msg) => {
    const newMsg = {
      role:'assistant',
      content:msg
    }
    messagesList.value.messages.push(newMsg)
  }

  const clearMessage = () => {
    messagesList.value.messages = initMessage
  }
  // 禁用功能
  const disabled = ref(false)
  return {
    messagesList,
    addUserMessage,
    sentMsg,
    addAssistantMessage,
    clearMessage,
    disabled
  }
})

